package com.example.myproject.client;

import com.google.gwt.user.client.Window;

public class Module implements Comparable<Module> {

    private int code;
    private String status;
    private int orient;
    private int xcoord;
    private int ycoord;
	
    public Module(int code, String status, int orient, int xcoord, int ycoord) {
		if (validModule(code)) {
			this.code = code;
		} else {
			Window.alert("Invalid Module Code: " + code);
			clear();
			return;
		}

		this.status = status;
		
		if (orient < 0 || orient > 2) {
			Window.alert("Impossible Orientation");
			clear();
			return;
		} else {
			this.orient = orient;
		}
		
		if (xcoord < 1 || xcoord > 100) {
			Window.alert("X Coordinate not allowed");
			clear();
			return;
		} else {
			this.xcoord = xcoord;
		}
		
		if (ycoord < 1 || ycoord > 50) {
			Window.alert("Y Coordinate not allowed");
			clear();
			return;
		} else {
			this.ycoord = ycoord;
		}		
    }
	
	public int getCode() {
		return code;
	}
	
	public String getStatus() {
		return status;
	}
	
	public int getOrient() {
		return orient;
	}
	
	public int getX() {
		return xcoord;
	}
	
	public int getY() {
		return ycoord;
	}
	
	public boolean moveTo(int x, int y) {
		if (x >= 1 && x <= 100 && y >= 1 && y <= 50) {
			this.xcoord = x;
			this.ycoord = y;
			return true;
		}
		return false;
	}
	
	public void turnUpright() {
		this.orient = 0;
	}
	
	public void setStatus(String status) {
		this.status = status;
	}	
	
	public String getType() {
		if (code >= 1 && code <= 40) {
			return "Plain";
		} else if (code >= 61 && code <= 80) {
			return "Dormitory";
		} else if (code >= 91 && code <= 100) {
			return "Sanitation";
		} else if (code >= 111 && code <= 120) {
			return "Storage";
		} else if (code >= 131 && code <= 134) {
			return "Gym";
		} else if (code >= 141 && code <= 144) {
			return "Canteen";
		} else if (code >= 151 && code <= 154) {
			return "Power";
		} else if (code >= 161 && code <= 164) {
			return "Control";
		} else if (code >= 171 && code <= 174) {
			return "Airlock";
		} else if (code >= 181 && code <= 184) {
			return "Medical";			
		} else {
			return "";
		}
	}
	
	public String toString() {
		String orientString;
		if (orient == 0) {
			orientString = "Upright";
		} else if (orient == 1) {
			orientString = "One turn needed";
		} else { //(orient == 2)
			orientString = "Two turns needed";
		}
		return "Module Code: " + code + "\n" +
			   "Module Type: " + getType() + "\n" +
			   "Status: " + status + "\n" +
			   "Orientation: " + orientString + "\n" +
			   "Coordinates: X: " + getX() + " Y: " + getY() + "\n";		
	}
	
	public String toSaveString() {
		return  "{code:" + code + 
				",status:\"" + status + 
				"\",turns:" + orient + 
				",X:" + xcoord + 
				",Y:" + ycoord + "}";
	}
	
	private void clear() {
		this.code = 0;
		this.status = "Unavailable";
		this.orient = 0;
		this.xcoord = 0;
		this.ycoord = 0;
	}
	
	private boolean validModule(int code) {
		if (code >= 1 && code <= 40) {
			return true;
		} else if (code >= 61 && code <= 80) {
			return true;
		} else if (code >= 91 && code <= 100) {
			return true;
		} else if (code >= 111 && code <= 120) {
			return true;
		} else if (code >= 131 && code <= 134) {
			return true;
		} else if (code >= 141 && code <= 144) {
			return true;
		} else if (code >= 151 && code <= 154) {
			return true;
		} else if (code >= 161 && code <= 164) {
			return true;
		} else if (code >= 171 && code <= 174) {
			return true;
		} else if (code >= 181 && code <= 184) {
			return true;
		} else {
			return false;
		}
	}

	@Override
	public int compareTo(Module mod) {
		String thisType = this.getType();
		String thatType = mod.getType();
		if (thisType == thatType) {
			return 0;
		}
		if (thisType == "Plain") {
			return -1;
		}
		if (thatType == "Plain") {
			return 1;
		}
		if (thisType == "Airlock") {
			return -1;
		}
		if (thatType == "Airlock") {
			return 1;
		}
		if (thisType == "Medical") {
			return -1;
		}
		if (thatType == "Medical") {
			return 1;
		}
		if (thisType == "Dormitory") {
			return -1;
		}
		if (thatType == "Dormitory") {
			return 1;
		}
		if (thisType == "Sanitation") {
			return -1;
		}
		if (thatType == "Sanitation") {
			return 1;
		}
		if (thisType == "Canteen") {
			return -1;
		}
		if (thatType == "Canteen") {
			return 1;
		}
		if (thisType == "Storage") {
			return -1;
		}
		if (thatType == "Storage") {
			return 1;
		}
		if (thisType == "Gym") {
			return -1;
		}
		if (thatType == "Gym") {
			return 1;
		}
		if (thisType == "Power") {
			return -1;
		}
		if (thatType == "Power") {
			return 1;
		}
		return 0;
	}
}